import akka.actor.Actor
import akka.actor.Props

class Supervisor extends Actor{
    var doneNodes = 0
    var nodes = 0
    var routeJumpList = List[Double]()
    
	def receive = {
      case Start(nOfNodes, nOfMessages) => 
        {
            nodes = nOfNodes
    		for (i <- 1 to nOfNodes){
    		  val nodeId = Utils.byteToHex(Utils.hash("akka://System/user/" + i))
    		  val a = context.system.actorOf(Props(classOf[Node], nodeId), name = i.toString)
    		  a ! Start(nOfNodes, nOfMessages)
    		  Thread.sleep(10)
    		}
           
            /*
            for (i <- 1 to nOfNodes){
    		  val a = context.system.actorSelection("akka://System/user/" + i)
    		  a ! PrintState
    		}
            */
            //val a = context.system.actorSelection("akka://System/user/1")
            //val key = Utils.byteToHex(Utils.hash("akka://System/user/390"))
            //a ! Route(key, "akka://System/user/1", 0)
        }
      case StartDone =>
        {
          doneNodes += 1
          if (doneNodes == nodes){
            //println("start Done")
            doneNodes = 0
            for (i <- 1 to nodes){
              context.system.actorSelection("akka://System/user/" + i) ! StartSending
            }
          }
        }
      case Done(avg) =>
        {
          //println("done received")
          doneNodes += 1
          routeJumpList ++= List(avg)
          if (doneNodes == nodes){
            val avg = Utils.calculateAVG(routeJumpList)
            println("Jump Average: " + avg)
            context.system.shutdown
          }
        } 
    }
}